package cmd

import (
	"context"
	"github.com/gogf/gf/v2/frame/g"
	"github.com/gogf/gf/v2/net/ghttp"
	"github.com/gogf/gf/v2/os/gcmd"
	"github.com/gogf/gf/v2/os/gctx"
	"github.com/gogf/gf/v2/os/gtime"
	"tFilm4/internal/controller"
)

func Upload(r *ghttp.Request) {
	files := r.GetUploadFiles("file")
	uploadPath := g.Cfg().MustGet(gctx.New(), "upload.path").String()
	if uploadPath == "" {
		r.Response.WriteExit(`上传文件路径配置不存在`)
	}
	dateDirName := gtime.Now().Format("Ymd")
	name, err := files.Save(uploadPath+"/"+dateDirName, true)
	if err != nil {
		r.Response.WriteExit(err)
	}
	r.Response.WritefExit("/upload/%s/%s", dateDirName, name[0])
}
func GetUpload(r *ghttp.Request) {
	r.Response.ServeFile(r.RequestURI)
}

var (
	Main = gcmd.Command{
		Name:  "main",
		Usage: "main",
		Brief: "start http server",
		Func: func(ctx context.Context, parser *gcmd.Parser) (err error) {
			s := g.Server()
			s.Group("/", func(group *ghttp.RouterGroup) {
				group.Middleware(ghttp.MiddlewareHandlerResponse)
				group.Bind(
					controller.User,
					controller.MovieHall,
					controller.Movie,
					controller.MovieSession,
					controller.MovieOrder,
					controller.Comment,
				)
				group.POST("/file", Upload)
				group.GET("/upload/*", GetUpload)
			})
			s.Run()
			return nil
		},
	}
)
